package com.jiarong.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * 封装MyBatis客户端代码
 *
 * @author 加绒
 * @date 2021/10/26 11:33
 */
public class SqlSessionUtil {
    /**
     * 获得SqlSession对象
     *
     * @return SqlSession对象
     */
    public static SqlSession getSession() {
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        return sqlSessionFactory.openSession(true);
    }
}
